<?php
session_start();
if(!isset($_SESSION['admin'])){
    header('Location: login.php');
}
if (isset($_REQUEST["continue"])) {
    $status = $_POST["selectbox"];
    $tenhinh = time() . $_FILES["hinhanh"]["name"];
    $duongdan = "images/gallery/" . $tenhinh;
    move_uploaded_file($_FILES["hinhanh"]["tmp_name"], $duongdan);
    require_once('database.php');
    $connection = new database;
    $sql = "insert into gallery (Link, Status) values ('" . $duongdan . "', '" . $status . "')";
    $connection->setQuery($sql);
    $result = $connection->query();
    if (!$result) {
        $result = '-1';
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="description" content="Reflect Template" />
        <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
        <title>Admin</title>
        <link rel="stylesheet" href="css/style_all.css" type="text/css" media="screen" />

        <!-- to choose another color scheme uncomment one of the foloowing stylesheets and wrap styl1.css into a comment -->
        <link rel="stylesheet" href="css/style1.css" type="text/css" media="screen" />
        <link rel="stylesheet" href="css/jquery-ui.css" type="text/css" media="screen" />
        <link rel="stylesheet" href="css/jquery.wysiwyg.css" type="text/css" media="screen" />
        <script type='text/javascript' src='js/jquery.js'></script>
        <script type='text/javascript' src='js/jquery-ui.js'></script>
        <script type='text/javascript' src='js/jquery.wysiwyg.js'></script>
        <script type='text/javascript' src='js/custom.js'></script>
        <script type='text/javascript' src='js/jquery.simplemodal.js'></script>
        <script type='text/javascript' src='js/basic.js'></script>
    </head>
    <body>
        <?php
        require_once("database.php");
        $connection = new database;
        $sql = "SELECT * from contact where Status = 0";
        $connection->setQuery($sql);

        $ar = $connection->loadAllRow();
        $sl = count($ar);
        $connection->disconnect();
        ?>
        <div id="dialog" title="Chào mừng bạn đến với trang quản lý admin">
            <p>Chào admin! Chào mừng bạn quay lại.<br/>
                Bạn có <strong><?php echo $sl; ?> thư mới</strong> trong hộp thư</p>
            <p>Đây là hộp thư liên hệ</p>
        </div>
        <div id="top">
            <div id="head">
                <h1 class="logo"> <a href="index.php">HOA VIET - Giao diện quản lý admin</a> </h1>
                <div class="head_memberinfo">
                    <div class="head_memberinfo_logo"> <span><?php echo $sl; ?></span> <img src="images/unreadmail.png" alt=""/> </div>
                    <span class='memberinfo_span'> Xin chào <a href="index.php">Admin</a> </span> <span class='memberinfo_span'> <a href="index.php">Cài đặt</a> </span> <span> <a href="login.php">Thoát</a> </span> <span class='memberinfo_span2'> <a href="index.php?tab=4">Bạn có <?php echo $sl; ?> thư mới</a> </span> </div>
                <!--end head_memberinfo--> 

            </div>
            <?php
            $limit = 5;
            $page = 1;
            if (isset($_GET["page"])) {
                $page = $_GET["page"];
                if ($page == "") {
                    $page = 1;
                }
            }
            require_once('database.php');
            $connection = new database;
            $sql = "SELECT * FROM gallery";
            $connection->setQuery($sql);
            $temp = $connection->loadAllRow();
            $total = count($temp);
            $max = $total / $limit;
            if ($total % $limit > 0) {
                $max++;
            }
            $start = ($page - 1) * $limit;
            ?>
            <div id="bg_wrapper">
                <div id="main">
                    <div id="content">
                        <div class="jquery_tab">
                            <div class="content_block">
                                <h2 class="jquery_tab_title">Danh sách hình ảnh</h2>
                                <p style="font-size: 15px"> Trang 
                                    <?php
                                    for ($i = 1; $i <= $max; $i++) {

                                        if ($i == $page) {
                                            echo"&nbsp;<a style='text-decoration:none' href=gallery.php?page=$i>$i</a>&nbsp;";
                                        } else {
                                            echo"&nbsp;<a href=gallery.php?page=$i>$i</a>&nbsp;";
                                        }
                                    }
                                    ?>
                                </p>
                                <table width="900" border="0" cellspacing="0" class="table_1">

                                    <tr class="title_list">
                                        <td width="5%">STT</td>
                                        <td width="70%">Hình ảnh</td>
                                        <td width="15%">Trạng thái</td>
                                        <td width="15%"></td>
                                    </tr>
                                    <?php
                                    $sql = "SELECT * FROM gallery order by ID desc limit $start,$limit";

                                    $connection->setQuery($sql);

                                    $ar = $connection->loadAllRow();
                                    $connection->disconnect();
                                    foreach ($ar as $a) {
                                        $id = $a["ID"];
                                        $link = $a["Link"];
                                        $status = $a["Status"];
                                        $symbol = "+";
                                        $title = "Đưa vào gallery";
                                        echo '<tr>';
                                        echo '<td>' . $id . '</td>';
                                        echo "<td><img src='" . $link . "' width='250px'></td>";
                                        if ($status == '1') {
                                            echo '<td>Gallery</td>';
                                            $symbol = "-";
                                            $title = "Loại khỏi vào gallery";
                                        } else {
                                            $symbol = "+";
                                            $title = "Đưa vào gallery";
                                            echo '<td>Un-Gallery</td>';
                                        }
                                        echo '<td align="center" style="text-align:center; padding:6px;">';
                                        echo '<input type="button" id="' . $id . '" name="basic" value="' . $symbol . '" class="button detail_button" title="' . $title . '" onclick = "return showdetailimage(this);"/>';
                                        echo '</td>';
                                        echo '</tr>';
                                    }
                                    ?>
                                </table>

                                <!-- preload the images -->
                                <div style='display:none'> <img src='images/basic/x.png' alt='' /> </div>
                            </div>
                            <!--end content_block--> 

                        </div>
                        <!--end jquery tab--> 
                        <div class="jquery_tab">
                            <div class="content_block">
                                <h2 class="jquery_tab_title">Thêm hình ảnh</h2>
                                <form method="post" action="gallery.php" enctype="multipart/form-data">
                                    <table width="675" border="0" cellspacing="0" class="table_3" style="margin-bottom:0;">
                                        <tr>
                                            <td width="30%">Tình trạng</td>
                                            <td>
                                                <select name="selectbox" id="selectbox_status" >
                                                    <option value = "1" style = "padding-left: 8px">Gallery</option>
                                                    <option value = "0" style = "padding-left: 8px">Un-Gallery</option>

                                                </select></td>
                                        </tr>
                                        <tr>
                                            <td>
                                                Chọn hình ảnh
                                            </td>
                                            <td>
                                                <input class="input-big" type="file" value="" name="hinhanh" id="hinhanh"/>
                                            </td>
                                        </tr>
                                    </table>
                                    <p style="padding-left: 11px; padding-top: 10px;">
                                        <input class="button" name="continue" type="submit" value="Thêm hình ảnh"/>
                                    </p>
                                </form>
                            </div>
                            <!--end content_block--> 

                        </div>
                        <!--end jquery tab-->
                    </div>
                    <!--end content--> 

                </div>
                <!--end main-->

                <div id="sidebar">
                    <ul class="nav">
                        <li><a class="headitem item1" href="index.php">Bảng điểu khiển</a> </li>
                        <li><a class="headitem item4" href="index.php">Quản lý chuyến du lịch</a></li>
                        <li><a class="headitem item7" href="files.php">Quản lý files</a> </li>
                        <li><a class="headitem item8" href="index.php?tab=4">Quản lý thư</a> </li>
                        <li><a class="headitem item9" href="gallery.php">Thiết lập gallery</a> </li>
                    </ul>
                    <!--end subnav-->

                    <div class="flexy_datepicker"></div>
                </div>
                <!--end sidebar--> 

            </div>
            <!--end bg_wrapper-->

            <div id="footer"> </div>
            <!--end footer--> 

        </div>
    </body>
</html>
